package com.ycsj.goldmedalnewconcept.teacher.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.PassDataInfo;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherAddQuestionsActivity;
import com.ycsj.goldmedalnewconcept.teacher.adapter.QuestionRvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.QuestionListInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import me.jessyan.autosize.internal.CancelAdapt;

/**
 * 题目列表的Fragment
 * Created by Administrator on 2019/2/21 0021.
 */

public class QuestionsListFragment extends BaseLazyFragment implements OnloadingMoreListener, SrfManager.OnPullRefreshListener, CancelAdapt {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srf)
    SmartRefreshLayout srf;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //封装的页面网络请求的参数
    private PassDataInfo info;

    //列表数据
    public List<TkItemBean> oldDatas;

    //列表适配器
    private QuestionRvAdapter adapter;

    //分页
    private int page;
    //空视图
    private View emptyView;

    /**
     * 生成当前Fragment的方法
     *
     * @param info
     * @return
     */
    public static QuestionsListFragment newInstance(PassDataInfo info) {
        QuestionsListFragment questionsListFragment = new QuestionsListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", info);
        questionsListFragment.setArguments(bundle);
        return questionsListFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.teacher_fragment_quetionlist;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        //关闭下拉刷新
        SrfManager.setSmartByPullListener(srf, null);

        //获取题型ID
        initintent();

        //设置列表
        initRv();

        //请求服务器数据
        http(BqaManager.NORML);

    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

//        rlLoading.setVisibility(View.GONE);

    }


    /**
     * 获取题型ID
     */
    private void initintent() {
        info = (PassDataInfo) getArguments().getSerializable("data");
    }

    /**
     * 请求服务器
     *
     * @param method
     */
    private void http(final int method) {

        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 0;
        } else {
            page++;
        }

        Map<String, String> map = new HashMap<>();
        map.put("type", "8");
        if (TextUtils.isEmpty(info.grade_D)) {
            map.put("grade_D", "");
        } else {
            map.put("grade_D", info.grade_D);
        }

        if (TextUtils.isEmpty(info.unit_D)) {
            map.put("unit_D", "");
        } else {
            map.put("unit_D", info.unit_D);
        }

        if (TextUtils.isEmpty(info.difficulty)) {
            map.put("difficulty", "");
        } else {
            map.put("difficulty", info.difficulty);
        }

        if (TextUtils.isEmpty(info.knowledgeAim_D)) {
            map.put("knowledgeAim_D", "");
        } else {
            map.put("knowledgeAim_D", info.knowledgeAim_D);
        }

        if (TextUtils.isEmpty(info.knowledge)) {
            map.put("knowledge", "");
        } else {
            map.put("knowledge", info.knowledge);
        }

        if (TextUtils.isEmpty(info.questionID)) {
            map.put("questionID", "");
        } else {
            map.put("questionID", info.questionID);
        }
        map.put("id", info.id + "");
        map.put("page", page + "");
        map.put("size", "10");

//        if ("5".equals(info.id + "")) {
//            LogUtils.iTag("test", "请求数据了");
//        }

        HttpUtils.post(HttpAddress.QUESTIONLIST, map, this, new HttpCallBack<QuestionListInfo>() {
            @Override
            public void onSuccess(QuestionListInfo info) {
                rlLoading.setVisibility(View.GONE);
                if ("200".equals(info.getState())) {
                    List<TkItemBean> newDatas = info.getData();
                    if (method == BqaManager.NORML) {
                        oldDatas.addAll(newDatas);
                        BqaManager.setRv(emptyView, getContext(), adapter, rv, QuestionsListFragment.this);

                    } else {
                        BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                    }
                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + info.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
                LogUtils.iTag("test", BaseApplication.ERROR);
            }
        });
    }


    /**
     * 设置列表的方法
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view_white_bg, null);
        TextView tvMsg = emptyView.findViewById(R.id.tv_content);
        tvMsg.setText("暂无数据");
        adapter = new QuestionRvAdapter((TeacherAddQuestionsActivity) getActivity(), getContext(), R.layout.item_rv_question, oldDatas);
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
//        http(BqaManager.PULLREFRESH);
    }
}
